package com.ruoyi.web.controller.cat;

import com.ruoyi.cat.domain.CatTradedetailTable;
import com.ruoyi.cat.domain.VO.OrderQueryVO;
import com.ruoyi.cat.domain.VO.SysOracleVO;
import com.ruoyi.cat.service.ICatTradedetailTableService;
import com.ruoyi.cat.service.OracleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.wxpay.WXPayUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "管理端-微信支付相关")
@RestController
@RequestMapping("/system/oracle")
public class OracleController  extends BaseController {

    @Autowired
    private OracleService oracleService;

    @Autowired
    private ICatTradedetailTableService  iCatTradedetailTableService;

    @PostMapping("/scanCodePayOrder")
    @ApiOperation("付款码收款")
    public AjaxResult scanCodePayOrder(@RequestBody SysOracleVO sysOracleVO) {
        return success(oracleService.scanCodePayOrder(sysOracleVO));
    }


    @PostMapping("/orderQuery")
    @ApiOperation("查询订单")
    public AjaxResult orderQuery(@RequestBody OrderQueryVO sysOracleVO) {
        return success(oracleService.orderQuery(sysOracleVO));
    }

    @PostMapping("/notify")
    public String payCallback(HttpServletRequest res, HttpServletResponse rsp) throws Exception {
        System.out.println("微信支付回调");
        InputStream inStream = res.getInputStream();
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        String resultxml = new String(outSteam.toByteArray(), "utf-8");
        logger.error("支付回调接口 {} ",resultxml);
        Map<String, String> params = WXPayUtil.xmlToMap(resultxml);
        logger.error("支付回调接口解析 {} ",params);
        outSteam.close();
        inStream.close();
        //支付结果返回，最后需要返回xml格式的数据
        Map<String, String> return_data = new HashMap<String, String>();
        if (!"SUCCESS".equals(params.get("return_code"))) {
            //没有支付成功
            return_data.put("return_code", "FAIL");
            return_data.put("return_msg", "return_code不正确");
            return WXPayUtil.mapToXml(return_data);
        } else {
            //返回成功了
            //商户订单号
            String outTradeNo = params.get("out_trade_no");
            //订单金额
            String totalFee = params.get("total_fee");
            //支付完成时间
            String timeEnd =params.get("time_end");
            String mch_id =params.get("mch_id");
            //微信支付订单号
            String tradeNo = params.get("transaction_id");
            CatTradedetailTable catTradedetailTable = new CatTradedetailTable();
            catTradedetailTable.setOrderNo(outTradeNo);
            catTradedetailTable.setStatus("1");
            iCatTradedetailTableService.updateCatTradedetailTableOrderNo(catTradedetailTable);
            System.out.println("商户订单号"+outTradeNo+"tradeNo"+tradeNo);
            return_data.put("return_code", "SUCCESS");
            return_data.put("return_msg", "OK");
            return WXPayUtil.mapToXml(return_data);
        }
    }

}
